package com.aaa.marketresearch.android.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.BillContent;
import com.aaa.marketresearch.android.data.domain.Offer;
import com.aaa.marketresearch.android.util.SimpleLog;

import java.util.List;

/**
 * @author Nail
 */
public class BillContentScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_content_screen);
    }

    @Override
    protected void onResume() {
        super.onResume();
        final BillContent billContent = UserInfo.getInstance().getCurrentBillContent();
        final Offer offer = billContent.getOffer();

        EditText payedValueEditText = (EditText) findViewById(R.id.offerPayedValueField);
        payedValueEditText.setText(billContent.getPayedValue().toString());

        EditText capacityEditText = (EditText) findViewById(R.id.offerCapacityField);


        TextView capacityTextView = (TextView) findViewById(R.id.offerCapacityText);
        if (offer.getCountable()) {
            capacityTextView.setText("количество");
            capacityEditText.setText(Integer.toString(billContent.getCapacity().intValue()));
        } else {
            String units = offer.getUnits();
            if (units.equalsIgnoreCase("KILOGRAM")) {
                capacityTextView.setText("масса, кг (пример '2.25')");
            } else if (units.equalsIgnoreCase("LITER")) {
                capacityTextView.setText("объем, литр (пример '1.5')");
            }
            capacityEditText.setText(billContent.getCapacity().toString());
        }
    }


    public void readyOfferButton(View view) {
        UserInfo userInfo = UserInfo.getInstance();
        BillContent billContent = userInfo.getCurrentBillContent();

        billContent.setFamilyMember(userInfo.getCurrentFamilyMember());
        billContent.setMarket(userInfo.getCurrentMarket());

        List<BillContent> bill = userInfo.getBill();

        EditText payedValueEditText = (EditText) findViewById(R.id.offerPayedValueField);
        EditText capacityEditText = (EditText) findViewById(R.id.offerCapacityField);

        double capacity = 0.0;
        try {
            capacity = Double.parseDouble(capacityEditText.getText().toString());
            billContent.setCapacity(capacity);
        } catch (NumberFormatException ex) {
            SimpleLog.error("ex " + ex);
            return;
        }

        double payedValue = 0.0;
        try {
            payedValue = Double.parseDouble(payedValueEditText.getText().toString());
            billContent.setPayedValue(payedValue);
        } catch (NumberFormatException ex) {
            SimpleLog.error("ex " + ex);
            return;
        }

        if (!bill.contains(billContent)) {
            bill.add(billContent);
        }

        Intent intent = new Intent(this, BillScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);

    }
}